package org.dromara.third.convert.elevationWork.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.ElevationWork;
import org.dromara.system.mapper.ElevationWorkMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.elevationWork.dto.ElevationWorkDTO;
import org.dromara.third.convert.elevationWork.validtor.ElevationWorkValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertElevationWorkController {

    @Autowired
    private ElevationWorkMapper elevationWorkMapper;

    @PostMapping("/elevationWork")
    @Transactional
    public R<Void> addElevationWork(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        ElevationWorkDTO elevationWorkDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            elevationWorkDTO = JSONObject.parseObject(decrypt, ElevationWorkDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, elevationWorkDTO, new ElevationWorkValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        ElevationWork elevationWork = assembleElevationWorkData(orgCode, elevationWorkDTO);
        int count = elevationWorkMapper.countWithOutDelFlag(elevationWork.getId());
        if (count == 0) {
            elevationWorkMapper.insert(elevationWork);
        } else {
            elevationWorkMapper.updateWithOutDelFlag(elevationWork);
        }
        return R.ok();
    }

    private ElevationWork assembleElevationWorkData(String orgCode, ElevationWorkDTO dto) {
        ElevationWork elevationWork = new ElevationWork();
        try {
            elevationWork.setId(dto.getZyid());
            elevationWork.setZyzbh(dto.getZyzbh());
            elevationWork.setSqdw(dto.getSqdw());
            elevationWork.setZysqsj(DateUtils.parseDate(dto.getZysqsj()));
            elevationWork.setSqr(dto.getSqr());
            elevationWork.setZynr(dto.getZynr());
            elevationWork.setZywz(dto.getZywz());
            elevationWork.setZygd(dto.getZygd());
            elevationWork.setZyjb(dto.getZyjb());
            elevationWork.setZyryxx(dto.getZyryxx());
            elevationWork.setZydwlx(dto.getZydwlx());
            elevationWork.setZydwmc(dto.getZydwmc());
            elevationWork.setZyfzr(dto.getZyfzr());
            elevationWork.setZysskssj(DateUtils.parseDate(dto.getZysskssj()));
            elevationWork.setZyssjssj(DateUtils.parseDate(dto.getZyssjssj()));
            elevationWork.setSfsjqttszy(dto.getSfsjqttszy());
            elevationWork.setSjqttszyzbh(dto.getSjqttszyzbh());
            elevationWork.setFxbs(dto.getFxbs());
            elevationWork.setAqjdr(dto.getAqjdr());
            elevationWork.setJsjdr(dto.getJsjdr());
            elevationWork.setJhr(dto.getJhr());
            elevationWork.setDqzt(dto.getDqzt());
            elevationWork.setLongitude(dto.getLongitude());
            elevationWork.setLatitude(dto.getLatitude());
            elevationWork.setStartTime(DateUtils.parseDate(dto.getStartTime()));
            elevationWork.setEndTime(DateUtils.parseDate(dto.getEndTime()));
            elevationWork.setSpgbbm(dto.getSpgbbm());
            elevationWork.setFhyp(dto.getFhyp());
            elevationWork.setAttachment(dto.getAttachment());
            elevationWork.setDelFlag(Long.parseLong(dto.getDelFlag()));
            elevationWork.setCreateDept(Long.parseLong(dto.getSsbm()));
            elevationWork.setCreateBy(Long.parseLong(dto.getCreateBy()));
            elevationWork.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            elevationWork.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            elevationWork.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            elevationWork.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装高处作业信息异常");
        }
        return elevationWork;
    }
}
